package cn.pli.bike.model.subscribers;


import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.framemodule.BaseApp.AppManager;
import com.framemodule.utils.Logger;


import java.net.ConnectException;
import java.net.SocketTimeoutException;

import cn.pli.bike.App;
import cn.pli.bike.model.http.ApiException;
import cn.pli.bike.model.progress.ProgressCancelListener;
import cn.pli.bike.model.progress.ProgressDialogHandler;
import cn.pli.bike.ui.CommonConstant;
import cn.pli.bike.ui.PhoneLogonActivity;
import rx.Subscriber;

/**
 * @author shangdongzhou
 * @date 2017/8/1
 */

public abstract class ProgramSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {

    private ProgressDialogHandler progressDialogHandler;

    public ProgramSubscriber(Context context) {
        if (context instanceof Service) {

        } else if (context instanceof Activity) {
            this.progressDialogHandler = new ProgressDialogHandler(context, this, true);
        }

    }

    public ProgramSubscriber() {

    }

    private void showProgressDialog() {
        if (progressDialogHandler != null) {
            progressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog() {
        if (progressDialogHandler != null) {
            progressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            progressDialogHandler = null;
        }
    }

    @Override
    public void onStart() {
        showProgressDialog();
    }

    @Override
    public void onCompleted() {
        dismissProgressDialog();
    }

    @Override
    public void onError(Throwable e) {

        if (e instanceof SocketTimeoutException) {
            // Toast.makeText(App.getApp().getApplicationContext(), "网络中断，请检查您的网络状态", Toast.LENGTH_SHORT).show();
            if (e.getMessage() == null) {
                return;
            }
            Logger.d(e.getMessage());
        } else if (e instanceof ConnectException) {
            // Toast.makeText(App.getApp().getApplicationContext(), "网络连接异常", Toast.LENGTH_SHORT).show();
            if (e.getMessage() == null) {
                return;
            }
            Logger.d(e.getMessage());
        } else {
            //  Toast.makeText(App.getApp().getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

            if (e instanceof ApiException) {
                switch (((ApiException) e).getCode()) {

                    case CommonConstant.CODE_OTHER_DEVICE:
                        Activity activity = AppManager.getAppManager().currentActivity();
                        App.getShareSetting().clear();
                        AppManager.getAppManager().finishAllActivity();
                        break;
                    case CommonConstant.CODE_1009:
                        break;
                    case CommonConstant.CODE_1010:
                        break;
                    case CommonConstant.CODE_TOKEN_ERR:
                        Activity activity1 = AppManager.getAppManager().currentActivity();
                        Intent intent1 = new Intent(activity1, PhoneLogonActivity.class);
                        App.getShareSetting().clear();
                        activity1.startActivity(intent1);
                        AppManager.getAppManager().finishAllActivity();

                        break;

                }

                Logger.d(((ApiException) e).getMsg());
                onErr((ApiException) e);
            } else {
                Logger.e("error", e.getMessage());
                e.printStackTrace();


            }
        }
        dismissProgressDialog();
    }


    @Override
    public void onCancelProgress() {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();//dialog不可打断链接
        }
    }

    public void onErr(ApiException e) {

    }

}
